package com.ktjy.controller;

import com.ktjy.entity.Page;
import com.ktjy.entity.R;
import com.ktjy.entity.SysUser;
import com.ktjy.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping("/page")
    public Page selectUserList(Integer pageNo, Integer pageSize, String account){
        SysUser sysUser=new SysUser();
        return userService.findPageVo(sysUser,pageNo,pageSize);
    }

    @RequestMapping("/add")
    public String add(){
        return "add";
    }

    @ResponseBody
    @PostMapping("/insert")
    public R insert(@RequestBody SysUser sysUser){
        int count = userService.insert(sysUser);
        return R.ok(count);
    }

    @ResponseBody
    @RequestMapping("/delete")
    public R delete(SysUser sysUser){
        int count = userService.delete(sysUser);
        return R.ok(count);
    }

    @ResponseBody
    @RequestMapping("/selectByIdUser")
    public SysUser selectByIdUser(SysUser sysUser){
        SysUser user = userService.selectByIdUser(sysUser);
        return user;
    }

    @ResponseBody
    @RequestMapping("/update")
    public R updateUser(@RequestBody SysUser sysUser){
        int i = userService.updateByIdUser(sysUser);
        return R.ok(i);
    }
    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @ResponseBody
    @PostMapping("/go")
    public R go(@RequestBody SysUser sysUser, HttpSession session){
        SysUser user;
        if(sysUser.getAccount()!=null && !sysUser.getAccount().isEmpty()){
            user=userService.selectAccount(sysUser);
            if(user==null){
                return R.fail();
            }
            if(user.getPassword().equals(sysUser.getPassword())){
                return R.ok(user);
            }else {
                return R.fail();
            }
        }
        return R.fail();
    }
}
